package darknet.gui.wizard.firstrun;

import darknet.DarknetRegistry;
import darknet.gui.MainFrame;
import java.io.File;
import javax.swing.JDialog;
import javax.swing.JPanel;

/**
 * A wizard to setup the application the first time it run.
 *
 * The wizard will ask some informations like username, password, address
 * and port.
 *
 * Once all informations are save, we create the facade (files are generated).
 * 
 * @author Yoyo
 */
@SuppressWarnings("serial")
public class WizardFirstRunDialog extends JDialog {

    /** The main JFrame of the application. */
    private MainFrame mainFrame;
    
    /** User name */
    private String username;
    
    /** password */
    private String password;
    
    /** Address */
    private String address;
    
    /** Port */
    private Integer port;

    /** Body panel. All other panel will be added to this panel */
    private JPanel body;

    /** Welcome panel */
    private WelcomePanel welcomePanel;

    /** Username and password setup panel */
    private UsernameAndPasswordPanel usernameAndPasswordPanel;

    /** Address and port setup panel */
    private NetworkPanel networkPanel;

    /** Panel showing tasks advancement */
    private GeneratePanel generatePanel;

    /** Final feedback panel */
    private FinishPanel finishPanel;

    /**
     * Initialise a new WizardFirstRunDialog.
     */
    public WizardFirstRunDialog(MainFrame mainFrame) {

        if (mainFrame == null) {
            throw new IllegalArgumentException("mainFrame is null");
        }
        this.mainFrame = mainFrame;
	
	// Configure some GUI options.
	setModal(true);
	setSize(700, 400);
	setTitle("Wizard");
        setResizable(false);
	
	// Create others Panel.
        this.body = new JPanel();
        this.welcomePanel = new WelcomePanel(this);
        this.usernameAndPasswordPanel = new UsernameAndPasswordPanel(this);
        this.networkPanel = new NetworkPanel(this);
        this.generatePanel = new GeneratePanel(this);
        this.finishPanel = new FinishPanel(this);

        this.add(body);
	showWelcome();
    }
    
    /**
     * Show the first Window of the Wizard.
     */
    public void showWelcome() {
	showPanel(welcomePanel);
    }
    
    /**
     * Show the username/password panel.
     */
    public void showUserAndPassword() {
	showPanel(usernameAndPasswordPanel);
    }
    
    /**
     * Show the network panel.
     */
    public void showNetwork() {
	showPanel(networkPanel);
    }

    /**
     * Show the generate panel.
     */
    public void showGenerate() {
        showPanel(generatePanel);
    }
    
    /**
     * Show the final panel.
     */
    public void showFinish() {
	showPanel(finishPanel);
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public MainFrame getMainFrame() {
        return mainFrame;
    }

    public FinishPanel getFinishPanel() {
        return finishPanel;
    }

    public GeneratePanel getGeneratePanel() {
        return generatePanel;
    }

    public NetworkPanel getNetworkPanel() {
        return networkPanel;
    }

    public UsernameAndPasswordPanel getUsernameAndPasswordPanel() {
        return usernameAndPasswordPanel;
    }

    public WelcomePanel getWelcomePanel() {
        return welcomePanel;
    }
    
    /**
     * Write parameters.
     */
    public void writeParameters() {

        String workingDirectory = System.getProperty("user.dir")
                + File.separator + "conf";

        DarknetRegistry.writeConfig(workingDirectory,
                username, password, address, port.toString());
    }

    /**
     * Attach a panel onto the body.
     *
     * @param panel
     */
    private void showPanel(JPanel panel) {

	this.invalidate();
        body.removeAll();
        body.add(panel);
	this.validate();
	this.repaint();
    }
}
